using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Unibot.Interfaces;
using LiquidFireArts.Wormhole.Misc;

namespace Unibot
{
    public enum PlayerState
    {
        None,
        Grounded,
        Jumping,
        Cooldown
    }

    public class Player : Element, ILivingObject
    {

        public PlayerIndex player_index; //player index
        public float HP { get; set; }
        public float defense { get; set; }

        public PlayerState player_state;

        public Player(PlayerIndex pi, Texture2D tx, Shape shp, Vector2 pos)
        {
            this.player_index = pi;
            this.texture = tx;
            this.TypeA = new List<Shape>() { shp };
            this.TypeB = new List<Shape>();
            this.player_state = PlayerState.None;
            this.physical_state = new PhysicalState();
            this.physical_state.position = pos;
            this.physical_state.friction_coeffecient = 0.9f;
            this.physical_state.elastic_coefficient = 0.5f;
            updateHitboxes();
        }
        
        public override void update(GameTime gt)
        {
            float interval = gt.ElapsedGameTime.Seconds * 0.1f; // needs mod, no idea wth I'm doing with gt but I feel like it might be necessary

            this.physical_state.acceleration += new Vector2(0, 0.8f); // gravity

            // keyboard inputs
            KeyboardState ks = Keyboard.GetState();
            if (ks.IsKeyDown(Keys.Right)) // move to the right
            {
                this.physical_state.acceleration += new Vector2(1,0);
            }
            else if (ks.IsKeyDown(Keys.Left)) // move to the left
            {
                this.physical_state.acceleration += new Vector2(-1, 0);
            }
            else if (ks.IsKeyDown(Keys.Down))
            {
                this.physical_state.acceleration += new Vector2(0, 1);
            }
            else if (ks.IsKeyDown(Keys.Up))
            {
                this.physical_state.acceleration += new Vector2(0, -1);
            }

            // Jump!
            if (ks.IsKeyDown(Keys.Space))
            {
                // but only if the player stays on the ground
                if (this.player_state != PlayerState.Jumping)
                {
                    this.physical_state.acceleration += new Vector2(0, -10);
                    this.player_state = PlayerState.Jumping;
                }
            }

            // change state
            updateState(gt);
            updateHitboxes();            
        }

        public override void Draw(SpriteBatch sb)
        {
            sb.Draw(texture, physical_state.position, Color.White);
        }

        public override void onCollisionWith(Element other, GameTime gt)
        {
            if (other.type == ElementType.Platform)
            {
                // special resolution:
                this.player_state = PlayerState.Grounded; // so he can jump again

                // collision resolution:
                this.physicalResolution(other, gt); // handles the rest
            }
            else if (other.type == ElementType.Enemy)
            {
                // etc.
            }
        }
    }
}
